app_family.controller("controller_orderlist", function($scope, $rootScope, httpRequest, $location) {
	$scope.imagebasepath = imagebasepath; // 资源图片根目录
	
	$rootScope.$broadcast('currentModule', 'page_orderlist');
	$rootScope.$broadcast('footerFlag', true); // 是否显示底部导航
	
	data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
	
	/**
	 * 显示哪一个tab
	 */
	var page_ordertype = data_sessionStorage.record_page.page_ordertype;
	if (page_ordertype === 'getOrderByFamily') { // 显示预定服务的tab
		$scope.orderType = 'service';
	} else {
		$scope.orderType = 'pay';
	}
	
	/**
	 * 判断是否用手机登录过
	 */
	if (data_sessionStorage) {
		var familyPhone = data_sessionStorage.familyInfo.phone;
		if (familyPhone == '' || familyPhone == null) {
			window.location.replace(basepath + 'login.html');
			return;
		}
	} else {
		window.location.replace(basepath + 'login.html');
		return;
	}
	
	/**
	 * tab控制
	 */
	$scope.showOrderType = function (typeStr) {
		$scope.orderType = typeStr;
	};
	
	/**
	 * 查询所有订单
	 */
	$scope.getOrderAll = function () {
		// 查询代付订单
		$scope.orderlist_forPaying = [{
			order_id: '132324343',
			order_status: 'ORDER_O2O_STATUS_PAYING',
			icon: '',
			sex: '',
			order_title: '科罗娜啤酒',
			order_time: '2017-02-03 12:34',
			user_name: 'Miya',
			junci_code: '45343434',
			sumMoney: '234',
			exclusive_key: 'dherwe232'
		}, {
			order_id: '132324343',
			order_status: 'finish',
			icon: '',
			sex: '',
			order_title: '百利甜酒',
			order_time: '2017-02-03 12:34',
			user_name: 'Miya',
			junci_code: '45343434',
			sumMoney: '234',
			exclusive_key: 'dherwe232'
		}];
		
		
		
			
		// 查询预定服务订单
		$scope.orderlist_byFamily = [];
	};
	
	// 查询所有订单
	$scope.getOrderAll();
	
	/**
	 * 订单操作 取消订单
	 */
	$scope.order_cancel = function (exclusive_key, order_id) {
		$.dialog.showDialog({
			msgTxt : '确定要取消订单吗？',
			isShowBtnCancel : true,
			callbackOk : function () {
				// 取消订单接口 调用
				// 刷新所有订单
				$scope.getOrderAll();
			}
		});
	};
	
	/**
	 * 订单操作 去支付
	 */
	$scope.order_pay = function (order_id, exclusive_key) {
		$.dialog.showDialog({
			isShowTitle : true,
			titleTxt : '在线去支付',
			msgTxt : '<div class="alipay-wrap"><img height="50" src="' + basepath + 'resources/images/alipay.png" /></div>',
			isShowBtnCancel : true,
			callbackOk : function () {
				// 根据订单，生成支付宝支付链接信息
				// 调用手机本地的支付宝应用  options:支付链接, 订单id, 排他key
			}
		});
		event.stopPropagation(); 
	};
	
	/**
	 * 获取支付结果
	 * resultFlag:支付结果
	 * msg:提示消息
	 * order_id:订单id
	 * exclusive_key:排他key
	 */
	$scope.getPayResult = function (resultFlag, msg, order_id, exclusive_key) {
		if (resultFlag === 'true') { // 支付成功
			$.dialog.showToast({
				msgTxt : msg,
				callbackOk : function () {
					// 家属支付成功，改变订单状态为已支付
					httpRequest.updateOrderStatusForPay({
						order_id : order_id,
						exclusive_key : exclusive_key
					}, function (data) {
						if (data.success === true) {
							// 刷新所有订单
							$scope.getOrderAll();
						} else if (data.success === false) {
							$.dialog.showToast({msgTxt : data.msg});
						}
					});
				}
			});
		} else { // 支付失败
			$.dialog.showToast({
				msgTxt : msg,
				callbackOk : function () {
					// 刷新所有订单
					$scope.getOrderAll();
				}
			});
		}
	};
	
	/**
	 * 查询订单详情
	 */
	$scope.getOrderDetail = function (orderId, ordertype) {
		if (orderId) {
			data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
	    	data_sessionStorage.record_page.orderdetail_id = orderId; // 保存要查看订单详情的orderid
	    	data_sessionStorage.record_page.page_ordertype = ordertype; // 指定订单列表显示代付订单的tab/预订服务订单的tab
	    	sessionStorage.setItem('data_sessionStorage', JSON.stringify(data_sessionStorage));
	    	
	    	$location.path('/orderdetail');
			$scope.$apply();
		} else {
			$.dialog.showToast({msgTxt : '订单有误。'});
		}
	};
});